<script setup>
import { useRouter, useRoute } from 'vue-router';
import { message } from 'ant-design-vue';

const router = useRouter();
const route = useRoute();
const refreshItem = () => {
  try {
    const currentPath = route.fullPath;
    router.replace({
      path: '/redirect' + currentPath,
      query: {
        t: Date.now(), // 添加时间戳避免缓存
        ...route.query,
      },
    });
  } catch (error) {}
};

/**
 * 刷新全部页面
 * 通过重定向到根路径再返回的方式实现全站刷新
 */
const refreshAll = () => {
  try {
    refreshItem();
  } catch (error) {
    message.error('刷新失败，请重试');
  }
};
</script>
<template>
  <a-dropdown trigger="hover" placement="bottomRight">
    <a-button type="text" class="btn-rotate" title="刷新选项">
      <template #icon>
        <y-icon class="fa-solid fa-arrows-rotate"></y-icon>
      </template>
    </a-button>
    <template #overlay>
      <a-menu class="refresh-menu">
        <a-menu-item key="refresh-current" @click="refreshItem">
          刷新当前
        </a-menu-item>
        <a-menu-divider />
        <a-menu-item key="refresh-all" @click="refreshAll">
          刷新全部
        </a-menu-item>
      </a-menu>
    </template>
  </a-dropdown>
</template>

<style scoped></style>
